<?php
/**
 * author      :chenwenwen
 * createTime  :2020-04-12 16:23
 * description :
 */
declare(strict_types=1);//严格模式
namespace app\common\business;

use app\common\lib\Num;
use app\common\lib\ClassArr;

class Sms {

    /**
     * 发送短信验证码
     * @param string $phone
     * @param string $code
     * @return bool
     */
    public static function sendCode(string $phoneNumber, int $len = 4, string $type = 'ali'): bool {
        //生成短信验证码
        $code = Num::getCode($len);
        //调用lib层发送验证码
        //$sms = AliSms::sendCode($phoneNumber, $code);

        //工厂模式实现
//        $type = ucfirst($type);
//        $class = 'app\common\lib\sms\\' . $type . 'Sms';
//        $sms = $class::sendCode($phoneNumber, $code);

        //反射机制实现
        $smsClassStat = ClassArr::smsClassStat();
        $smsClassObj = ClassArr::initClass($type, $smsClassStat, [], false);
        $sms = $smsClassObj::sendCode($phoneNumber,$code);
        if (true === $sms) {
            //将验证码存入redis 给定一个失效时间
            cache(config('redis.sms_code_pre') . $phoneNumber, $code, config('redis.sms_code_expire'));
        }
        return $sms;

    }
}